//
//  PositionDetailViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/10.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "PositionDetailViewController.h"
#import "PositionModel.h"
#import "CreateResumeVC.h"
#import "StartYQViewController.h"
@interface PositionDetailViewController ()
{
    CGFloat right;
    UIView * lineV;
    NSMutableArray * dataSource;
    NSMutableArray * dataSource2;
    
    CGFloat totleW;
    
    NSMutableArray * btnArr;
    NSMutableArray * selectedBtnArr;
    
    UIScrollView * myScrollView;
}
@end

@implementation PositionDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
   // [self addBackItem];
    self.navigationItem.title = _titles;
    //[self addNavgationBar];
    // Do any additional setup after loading the view.
}
-(void)main
{
    dataSource = [[NSMutableArray alloc]init];
    dataSource2 = [[NSMutableArray alloc]init];
    btnArr = [[NSMutableArray alloc]init];
    selectedBtnArr = [[NSMutableArray alloc]init];
    
    myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64+43, SCREEN_WIDTH, SCREEN_HEIGHT-64-43)];
    [self.view addSubview:myScrollView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self requestData];
    
}
-(void)requestData
{
    __weak typeof(self)weakSelf = self;
    [U76HONet selectedTypeReadWithtype:@"3" fid:_code code:self._typeid andDataBlock:^(NSArray *arr) {
        NSLog(@"222级职位分类获取数据成功了");
        [dataSource addObjectsFromArray:arr];
        [weakSelf createTopScrollViewwithArr:arr];
    }];
    
}
-(void)requestDatawithcode:(NSString *)code typeId:(NSString *)typeid
{
    __weak typeof(self)weakSelf = self;
    [U76HONet selectedTypeReadWithtype:@"3" fid:code code:typeid andDataBlock:^(NSArray *arr) {
        NSLog(@"333级职位分类获取数据成功了");
        if (selectedBtnArr.count!=0) {
            for (UIButton * btn in selectedBtnArr) {
                [btn removeFromSuperview];
            }
        }
        if (dataSource2.count!=0) {
            [dataSource2 removeAllObjects];
        }
        [dataSource2 addObjectsFromArray:arr];
        NSMutableArray * mArray = [[NSMutableArray alloc]init];
        for (int i = 0; i<arr.count; i++) {
            PositionModel * model = arr[i];
            [mArray addObject:model.typenames];
        }
        [weakSelf createMainViewwithData:mArray];
    }];
}
-(void)createTopScrollViewwithArr:(NSArray *)Arr
{
    NSMutableArray * arr = [[NSMutableArray alloc]init];
    for (int i = 0; i<Arr.count; i++) {
        PositionModel * model = Arr[i];
        [arr addObject:model.typenames];
        if (i == 0) {
            [self requestDatawithcode:model.Code typeId:model.Typeid];
        }
    }
    right = 0;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    NSString * str = [arr componentsJoinedByString:@""];
    CGFloat width = [str widthOfSize:22];
    NSLog(@"width:%i",(int)width);
    
    UIScrollView * scroll= [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 43)];
    scroll.bounces = NO;
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    scroll.backgroundColor = [UIColor whiteColor];
    scroll.userInteractionEnabled = YES;
    scroll.contentSize = CGSizeMake(width, 0);
    scroll.contentOffset = CGPointMake(0, 0);
    [self.view addSubview:scroll];
    [scroll setContentSize:CGSizeMake(width, scroll.frame.size.height)];
    
    UIImageView * topView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, 43)];
    topView.backgroundColor = [UIColor whiteColor];
    topView.userInteractionEnabled = YES;
    [scroll addSubview:topView];
    
    for (int i = 0; i<arr.count; i++) {
        NSString * s = arr[i];
        CGFloat littleW = [s widthOfSize:22];
        NSLog(@"right:%i",(int)right);
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnArr addObject:btn];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        btn.frame = CGRectMake(right, 0, littleW, 43);
        btn.tag = 100+i;
        [btn setTitleColor:UIColorFromRGB(0x94999f) forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0x6ec844) forState:UIControlStateDisabled];
        [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:btn];
        right = btn.right;
        
        if (i == 0) {
            btn.enabled = NO;
            lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 42, littleW, 1)];
            lineV.backgroundColor = UIColorFromRGB(0x6ec844);
            [topView addSubview:lineV];
        }
        
    }

}
-(void)pressBtn:(UIButton *)btn
{
    for (UIButton * b in btnArr) {
        if (b == btn) {
            b.enabled = NO;
        }
        else
        {
            b.enabled = YES;
        }
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        lineV.frame = CGRectMake(btn.left, 42, btn.width, 1);
    } completion:nil];
    
    int i = (int)btn.tag - 100;
    PositionModel * model = dataSource[i];
    [self requestDatawithcode:model.Code typeId:model.Typeid];

    

}
#pragma mark - createMainView
-(void)createMainViewwithData:(NSArray *)dataArr
{
    totleW = 0;
    CGFloat w = SCREEN_WIDTH-SIZETO6W(20)*2;
    int j = 0;
    for (int i = 0; i<dataArr.count; i++) {
        NSString * str = dataArr[i];
        CGFloat tempW = [str widthOfSize:20];
        totleW = totleW+tempW+SIZETO6W(15);
        if (totleW<=w) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(SIZETO6W(20)+totleW-tempW-SIZETO6W(15), SIZETO6H(23)+j*(40+SIZETO6H(15)), tempW, 40);
            [btn setTitle:dataArr[i] forState:UIControlStateNormal];
            [btn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(selectedBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.backgroundColor = [UIColor whiteColor];
            btn.tag = i+1000;
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 5;
            [selectedBtnArr addObject:btn];
            [myScrollView addSubview:btn];
            if (i == 0) {
                [btn setBackgroundColor:UIColorFromRGB(0x6ec844)];
                [btn setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];

            }
            if (i == dataArr.count-1) {
                myScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, btn.bottom+10);
            }
        }
        else
        {

            j = j+1;
            totleW = tempW+SIZETO6W(15);
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:dataArr[i] forState:UIControlStateNormal];
            [btn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(selectedBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [selectedBtnArr addObject:btn];
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 5;
            btn.frame = CGRectMake(SIZETO6W(20),SIZETO6H(23)+j*(40+SIZETO6H(15)), tempW, 40);
            btn.backgroundColor = [UIColor whiteColor];
            btn.tag = i+1000;
            [myScrollView addSubview:btn];
            
            if (i == dataArr.count-1) {
                myScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, btn.bottom+10);
            }

        }
        
    }
}
-(void)selectedBtnAction:(UIButton *)btn
{
    for (UIButton * b in selectedBtnArr) {
        if (btn == b) {
            [b setBackgroundColor:UIColorFromRGB(0x6ec844)];
            [b setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        }
        else
        {
            [b setBackgroundColor:UIColorFromRGB(0xffffff)];
            [b setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
        }
    }
    PositionModel * model = dataSource2[btn.tag-1000];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setValue:model.Ids forKey:@"ids"];
    [dic setValue:model.typenames forKey:@"typenames"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"selectedZhiweiNotification" object:dic];
    for (UIViewController * VC in self.navigationController.viewControllers) {
        if ([VC isKindOfClass:[CreateResumeVC class]]) {
            [self.navigationController popToViewController:VC animated:YES];
        }
    }
    
    
    
    
    for (UIViewController * VC in self.navigationController.viewControllers) {
        if ([VC isKindOfClass:[StartYQViewController class]]) {
            [self.navigationController popToViewController:VC animated:YES];
        }
    }

}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
